package com.mt.lulucai.log;

/**
 * Created by daiguangtao on 2017/7/10.
 */

import android.os.Environment;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogWriter {

    private static LogWriter mLogWriter;

    private String mPath;
    private static final int MEMORY_LOG_FILE_MAX_SIZE = 10 * 1024 * 1024;           //内存中日志文件最大值，10M
    private static SimpleDateFormat df;


    private LogWriter() {
        String file_path = Environment.getExternalStorageDirectory().getPath();
        this.mPath = file_path+"/mthealthlog";
        File mFile = new File(mPath);
        if(!mFile.exists()){
            mFile.mkdirs();
        }
        try{
            df = new SimpleDateFormat("[yy-MM-dd hh:mm:ss]: ");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String getmPath() {
        return mPath;
    }


    public static LogWriter getinstance() {
        if (mLogWriter == null) {
            mLogWriter = new LogWriter();
        }
        return mLogWriter;
    }

    /**
     * 检查日志文件大小是否超过了规定大小
     * 如果超过了新生成文件
     */
    private File getLogFile(String logNmae){
        String path = mPath + File.separator + logNmae;
        File file = new File(path);
        if(file.length() >= MEMORY_LOG_FILE_MAX_SIZE){
            file.renameTo(new File(mPath + File.separator + logNmae+ new SimpleDateFormat("yyMMddhhmmss")));
            File mFile = new File(path);
            return mFile;
        }else{
            return file;
        }
    }



    public void printInfo(String log){
        Writer mWriter = null;
        try{
            mWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(getLogFile("info.log"), true)));
            mWriter.write(df.format(new Date()));
            mWriter.write(log);
            mWriter.write("\n");
            mWriter.flush();
            mWriter.close();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if(mWriter!=null){
                    mWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void printInfo(Class cls, String log) { //如果还想看是在哪个类里可以用这个方法
        Writer mWriter = null;
        try{
            mWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(getLogFile("info.log"), true)));
            mWriter.write(df.format(new Date()));
            mWriter.write(cls.getSimpleName() + " ");
            mWriter.write(log);
            mWriter.write("\n");
            mWriter.flush();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if(mWriter!=null){
                    mWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void printSendInfo(Class cls, String log) { //如果还想看是在哪个类里可以用这个方法
        Writer mWriter = null;
        try{
            mWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(getLogFile("send_info.log"), true)));
            mWriter.write(df.format(new Date()));
            mWriter.write(cls.getSimpleName() + " ");
            mWriter.write(log);
            mWriter.write("\n");
            mWriter.flush();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if(mWriter!=null){
                    mWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void printError(String log){
        Writer mWriter = null;
        try{
            mWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(getLogFile("error.log"), true)));
            mWriter.write(df.format(new Date()));
            mWriter.write(log);
            mWriter.write("\n");
            mWriter.flush();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if(mWriter!=null){
                    mWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void printError(Class cls, String log){ //如果还想看是在哪个类里可以用这个方法
        Writer mWriter = null;
        try{
            mWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(getLogFile("error.log"), true)));
            mWriter.write(df.format(new Date()));
            mWriter.write(cls.getSimpleName() + " ");
            mWriter.write(log);
            mWriter.write("\n");
            mWriter.flush();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if(mWriter!=null){
                    mWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public String getLogPath(){
        return this.mPath;
    }
}